<template>
  <div style="width: 100%; height: 100%">

    <!-- v-on="gridEvents" overflow: hidden -->
    <vxe-grid ref="tableRef" v-bind="gridOptions">
      <template #toolbar_buttons>
        <Button type="primary" @click="handleCreate">发布试卷</Button>
      </template>
      <template #action="{ row }">
        <TableAction outside :actions="getActions(row)" />
      </template>

    </vxe-grid>


    <FormModal @register="registerDrawer" @success="handleSuccess" />
    <addFormModal  @register="addModal[0]" @success="handleSuccess"></addFormModal>
    <UserAnserLogDrawer @register="userAnser[0]"></UserAnserLogDrawer>
    <Modal v-model:visible="applyViewShow" title="" @ok="download" >
      <div style="width: 100%;text-align: center">
<!--        style="padding-left: 12%;font-weight: bold;font-size: 20px"-->
        <div style="font-weight: bold;font-size: 20px">{{title}}</div>
        <QrCode :value="qrCodeUrl" />
      </div>
      <div style="margin-top: 20px">
        {{qrCodeUrl}}
      </div>
<!--      <div>-->
<!--        <span style="color: red">(注意)</span><span>经量不要用微信扫一扫，请选择 浏览器上的扫一扫功能</span>-->
<!--      </div>-->
    </Modal>

  </div>
</template>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { publishPaperList, removePublishPaper,publishPaperActive } from '@/api/heng/ex';
import { QrCode, QrCodeActionType } from '/@/components/Qrcode/index';

import {
  BasicTableProps,
  VxeBasicTable,
  VxeGridInstance,
  VxeGridListeners,
  VxeTableDefines,
} from '@/components/VxeTable';
import {
  vxeTableColumns,
  vxeTableFormSchema,
} from './role.data';
import { isProdMode } from '@/utils/env';

import { Tag, Descriptions, DescriptionsItem } from 'ant-design-vue';
import { reactive, ref, unref, onMounted } from 'vue';
import { useUserStore } from '@/store/modules/user';
import FormModal from './FormModal.vue';
import addFormModal from './addFormModal.vue';
import { useMessage } from '@/hooks/web/useMessage';
import { useModal } from '@/components/Modal';
import { useDrawer } from '@/components/Drawer';
import UserAnserLogDrawer from './UserAnserLogDrawer.vue';

import { Modal, Tooltip, Button, Timeline, TimelineItem, MonthPicker } from 'ant-design-vue';

defineOptions({ name: 'MouldList' });
const { createMessage } = useMessage();
const userStore = useUserStore();
const [registerDrawer, { openDrawer }] = useDrawer();
const qrCodeUrl=ref("");
const addModal = useDrawer();
const userAnser= useDrawer();
const loading = ref(false);
const applyViewShow = ref(false);
const title=ref("");


const tableRef = ref<VxeGridInstance>();

const gridOptions = reactive<BasicTableProps>({
  // keepSource: true,
  editConfig: { trigger: 'click', mode: 'cell', showStatus: true },
  border: true,
  showOverflow: true,
  size: 'mini',
  height: 'auto', // 'auto'
  columns: vxeTableColumns,
  filterConfig: {
    remote: true,
  },

  toolbarConfig: {
    slots: {
      buttons: 'toolbar_buttons',
    },
    refresh: true,
  },
  formConfig: {
    // enabled: true,
    items: vxeTableFormSchema,
  },
  pagerConfig: {
    // pageSize: 20,
  },
  proxyConfig: {
    props: {
      result: 'data.list',
      list: 'data.list',
      total: 'data.totalResult',
      message: 'message',
    },
    filter: true,
    ajax: {
      query: ({ page, form, filters }) => {
        return findPageList(page, form, filters);
      },
    },
  },
});

function findPageList(page, form, filterList?: VxeTableDefines.FilterCheckedParams<any>[]) {
  let json = {};
  if (filterList && filterList.length) {
    filterList.forEach((e) => {
      const value = e.datas[0];
      let field = e.field;
      json[field + 'Like'] = value;
    });
  }
  // console.log("----json:", json);
  // ywApproveList xftList
  return publishPaperList({
    page: page.currentPage,
    pageSize: page.pageSize,
    ...form,
    ...json,
  });
}

const findList = (filterList?: VxeTableDefines.FilterCheckedParams<any>[]) => {
  gridOptions.loading = true;
  // 模拟接口
  let queryJson = {};
  if (filterList && filterList.length) {
    filterList.forEach((e) => {
      const value = e.datas[0];
      let field = e.field;
      queryJson[field + 'Like'] = value;
    });
  }

  return new Promise<any[]>((resolve) => {
    let pp = {
      page: { currentPage: 1, pageSize: 20 },
      form: queryJson,
    };

    gridOptions.proxyConfig.ajax.query(pp).then((res) => {
      gridOptions.loading = false;
      let resultObj = {
        result: res.data.list,
        page: {
          total: res.data.totalResult,
        },
      };

      resolve(res);
    });
  });
};





function applyViewOpen() {
  applyViewShow.value = true;
}

function applyViewClose() {
  applyViewShow.value = false;
}



function download(){
  const qrRef = ref<Nullable<QrCodeActionType>>();

  const qrEl = unref(qrRef);
  if (!qrEl) return;
  qrEl.download(title.value).then((res) => {
    applyViewClose();
  });
}



function getActions(record) {
  let arr = [
    {
      // icon: 'clarity:note-edit-line',
      label: '二维码',
      onClick: handleQrCode.bind(null, record),
    },
    {
      // icon: 'clarity:note-edit-line',
      label: '设置',
      onClick: handleEdit.bind(null, record),
    },
    {
      // icon: 'clarity:note-edit-line',
      label: '查看答题',
      onClick: userAnserFn.bind(null, record),
    },
    {
      // icon: 'ant-design:delete-outlined',
      label: '禁用',
      color: 'error',
      popConfirm: {
        title: '是否禁用？',
        placement: 'left',
        confirm: handleClose.bind(null, record),
      },
    }
  ];


  return arr;
}

function handleCreate() {
  addModal[1].openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    isUpdate:true,
    record
  });
}

function userAnserFn(record: Recordable) {
  userAnser[1].openDrawer(true, record);
}
/**
 * 生成二维码
 * @param record
 */
function handleQrCode(record: Recordable){
  let url='';
  if(isProdMode()){
    url='http://220.161.77.66:10111/h5/#/';
  }else{
    url='http://192.168.0.173:8080/#/';
  }

  qrCodeUrl.value=`${url}?publishPaperId=${record.publishPaperId}`;
  title.value=`${record.paperName}`;
  applyViewOpen();
}

function handleClose(record: Recordable) {
  //console.log(record);
 // removePublishPaper
  publishPaperActive({publishPaperId:record.publishPaperId,active:0})
    .then((res) => {
      if (res.code == 200) {
        createMessage.success(res.message);
        tableRef.value.commitProxy('reload');
      } else {
        createMessage.error(res.message);
      }
    })
    .catch((res) => {
      createMessage.error(res.message);
    })
    .finally(() => {
    });
}




function handleSuccess() {
  tableRef.value.commitProxy('reload');
}



</script>
<style lang="less">
.isReturn_clazz {
  text-align: center;
  cursor: pointer;
}
.qrcode-demo-item {
  width: 30%;
  margin-right: 1%;
}
</style>
